package UserController

import (
	"QingDai-Gin/api/entities"
	"QingDai-Gin/api/models"
	"QingDai-Gin/service/UserService"
	"QingDai-Gin/utils/JwtUtil"
	"github.com/gin-gonic/gin"
)

// SendTextMessage
// @Tags 用户
// @Summary 发送短信验证码
// @Router /user/textMessage [get]
// TODO
func SendTextMessage(context *gin.Context) {
	context.JSON(200, entities.CommonResult{Code: 200, Message: "发送短信验证码成功", Data: nil})
}

// CreateUser
// @Tags 用户
// @Summary 创建新用户
// @Router /user/ [post]
func CreateUser(context *gin.Context) {
	name, _ := context.Get("name")
	username, _ := context.Get("username")
	password, _ := context.Get("password")
	phone, _ := context.Get("phone")
	user := models.User{
		Name:     name.(string),
		Username: username.(string),
		Password: password.(string),
		Remark:   "",
		Phone:    phone.(string),
	}
	UserService.CreateUser(user)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "创建新用户成功", Data: nil})
}

// UpdateUserByJwt
// @Tags 用户
// @Summary 更新个人用户
// @Router /user/jwt [put]
func UpdateUserByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	name, _ := context.Get("name")
	username, _ := context.Get("username")
	password, _ := context.Get("password")
	phone, _ := context.Get("phone")
	user := models.User{
		ID:       UserId,
		Name:     name.(string),
		Username: username.(string),
		Password: password.(string),
		Remark:   "",
		Phone:    phone.(string),
	}
	UserService.UpdateUser(user)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "更新个人用户成功", Data: nil})
}

// GetUserByJwt
// @Tags 用户
// @Summary 获取个人用户
// @Router /user/jwt [get]
func GetUserByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	user, _ := UserService.GetUserById(UserId)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "获取个人用户成功", Data: user})
}

// DeleteUserByJwt
// @Tags 用户
// @Summary 删除个人用户
// @Router /user/jwt [delete]
func DeleteUserByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	UserService.DeleteUser(models.User{ID: UserId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "删除个人用户成功", Data: nil})
}
